<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>创建或修改文章</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../layui/css/layui.css" media="all">
</head>
<body>
  <form id="post_form" class="layui-form" action="" lay-filter="post_form" style="padding:20px;">
    <div class="layui-form-item">
      <label class="layui-form-label">关键字</label>
      <div class="layui-input-inline">
      	<div id="tags">
			<input type="text" name="" id="inputTags" placeholder="回车生成标签" autocomplete="off">
		</div>
	  </div>
    </div>

    <div class="layui-form-item">
      <label class="layui-form-label">日报摘要</label>
      <div class="layui-input-inline" style="width:600px;">
      	<textarea name="digest" lay-verify="digest" class="layui-textarea"></textarea>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">日报内容</label>
      <div class="layui-input-inline" style="width:600px;">
          <textarea name="content" lay-verify="content"  id="content" style="display: none;"></textarea>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit="" lay-filter="post_form">立即提交</button>
        <a href="daily_list.html" class="layui-btn layui-btn-primary">返回</a>
      </div>
    </div>
  </form>
  
  <script src="../layui/layui.js"></script>  
  <script>
  layui.use(['form', 'layedit', 'inputTags'], function(){
	var $ = layui.$, form = layui.form;
	// var $ = layui.jquery;

    var layedit = layui.layedit;
    var editorIndex = layedit.build('content'); //建立编辑器
    
    
    var keywords = [];
    var inputTags = layui.inputTags;
    inputTags.render({
      elem:'#inputTags',
      content: keywords
    });
    
    // 提交数据注意点：
    // 1. 表单项必须要有name属性
    // 2. 提交按钮的lay-filter="post_form"，和form.on(submit(post_form)，否则取不到数据
    // 3. 富文本编辑器的内容要同步到textarea里面，
    // 4. 输入项需要校验的话，需要加上lay-verify="content" 
    
    
    //监听提交
    form.on('submit(post_form)', function(data){
      var field = data.field; //获取提交的字段
      
      field['keywords'] = keywords;
      
      $.post("../dailies/add", $.param(field, true), function() {
    	  window.location.href = 'daily_list.html';
      });
      return false;
    });

    //自定义验证规则
    form.verify({
      digest: function(value) {
        if(value.length < 5) {
          return '摘要至少5个字符';
        }
      },
      content: function(value) {
        layedit.sync(editorIndex);
      }
    });
    
    //表单初始赋值
    form.val('post_form', {
      // "category": "1"
    });

  })
  </script>
</body>
</html>